//
//  XJCommunityHistoryView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/5.
//  Copyright © 2018年 Tzyang. All rights reserved.
//
//  历史记录view
#import "XJCommunityHistoryView.h"

// Model
#import "XJCommunityModel.h"

@interface XJCommunityHistoryView()
@property(nonatomic, strong) UILabel *lastLabel;
@end

@implementation XJCommunityHistoryView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor redColor];
    }
    return self;
}



- (void)setHistoryArr:(NSMutableArray *)historyArr {

    _historyArr = historyArr;
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if (_historyArr.count == 0) {
        self.hidden = YES;
        return;
    }
    
    
    self.hidden = NO;
    CGFloat s_w = self.frame.size.width;
    CGFloat kTagTitleHeight = 22.0f; // 标题高度
    
    CGFloat margin = 10;
    CGFloat w = (KWidth - 10*8)/4;
    
    // 热门搜索
    UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(margin, 10, s_w, kTagTitleHeight)];
    [self addSubview:tipsLabel];
    tipsLabel.text = @"搜索历史";
    // 删除按钮
    UIButton *deleteButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    deleteButton.frame = CGRectMake(s_w - kTagTitleHeight*2, 10, kTagTitleHeight, kTagTitleHeight);
    [deleteButton setImage:[UIImage imageNamed:@"删除"] forState:(UIControlStateNormal)];
    [deleteButton addTarget:self action:@selector(deleteButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:deleteButton];
    
    for (int i=0; i < historyArr.count; i++) {
        UILabel *lb = [UILabel new];
        lb.layer.masksToBounds = YES;
        lb.layer.cornerRadius = 5;
        lb.layer.borderColor = [UIColor blackColor].CGColor;
        lb.layer.borderWidth = 1.0;
        XJCommunityModel *model = historyArr[i];
        lb.text = model.name;
        lb.tag = i;
        
        CGFloat x = margin;
        if (i>0) {
            x = margin + (2*margin + w) * i;
        }
        
        lb.frame = CGRectMake(x, tipsLabel.rb_bottom + 10, w, kTagTitleHeight);
        [self addSubview:lb];
        
        
        lb.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLbGesgure:)];
        [lb addGestureRecognizer:tapGes];
    }
    
}


//- (void)setHistoryArr:(NSMutableArray *)historyArr {
//    _historyArr = historyArr;
//
//    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//
//    if (_historyArr.count == 0) {
//        self.hidden = YES;
//        return;
//    }
//
//
//    self.hidden = NO;
//    CGFloat s_w = self.frame.size.width;
//    CGFloat kTagTitleHeight = 20.0f; // 标题高度
//
//    // 热门搜索
//    UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, s_w, kTagTitleHeight)];
//    [self addSubview:tipsLabel];
//    tipsLabel.text = @"搜索历史";
//    // 删除按钮
//    UIButton *deleteButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    deleteButton.frame = CGRectMake(s_w - 60, 10, kTagTitleHeight, kTagTitleHeight);
//    [deleteButton addTarget:self action:@selector(deleteButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
//    [self addSubview:deleteButton];
//
//    // 复原布局
//    _lastLabel = nil;
//
//    CGFloat kTitleHorizontal_space =  10.0f; // 字体离边框的水平距离
//    CGFloat kTitleVertical_space = 5.0f; // 字体边框的垂直距离
//    CGFloat kTagScreen_margin = 12.f; /** tagLab与屏幕左右间距 */
//
//    CGFloat kTagHorizontal_margin = 15.0f; /** tagLab之间的水平间距 */
//    CGFloat kTagVertical_margin = 10.0f; /** tagLab之间的竖直间距 */
//
//
//
//    for (int i = 0; i<_historyArr.count; i++) {
//        XJCommunityModel *model = _historyArr[i];
//
//        NSString *textStr = model.name;
//
//
//        UILabel *tagLabel = [UILabel new];
//
//        tagLabel.text = textStr;
//        UIFont *kTagFont =  [UIFont boldSystemFontOfSize:13];
//
//
//        CGSize tagSize = [textStr sizeWithAttributes:@{NSFontAttributeName:kTagFont}];
//
//        CGFloat w = tagSize.width + kTitleHorizontal_space *2;
//        CGFloat h = tagSize.height + kTitleVertical_space *2;
//
//
//        if (i == 0) { // 固定第一个元素位置
//            tagLabel.frame = CGRectMake(kTagScreen_margin, kTagTitleHeight*2, w, h);
//        }else {
//            CGFloat maxRight = CGRectGetMaxX(_lastLabel.frame) + kTagHorizontal_margin + w;
//
//            if (maxRight > s_w - kTagVertical_margin) {
////                tagLabel.frame = CGRectMake(kTagScreen_margin, CGRectGetMaxY(_lastLabel.frame) + kTagVertical_margin, w, h);
//                tagLabel.frame = CGRectMake(kTagScreen_margin, CGRectGetMaxY(_lastLabel.frame)+kTagVertical_margin, w, h);
//
//            }else {
////                tagLabel.frame = CGRectMake(CGRectGetMaxX(_lastLabel.frame) + kTagHorizontal_margin, CGRectGetMaxY(_lastLabel.frame), w, h);
//                tagLabel.frame = CGRectMake(CGRectGetMaxX(_lastLabel.frame)+kTagHorizontal_margin, CGRectGetMinY(_lastLabel.frame), w, h);
//
//            }
//        }
//
//        [self addSubview:tagLabel];
//        _lastLabel = tagLabel;
//
//
//        [self configLabel:tagLabel];
//
//        // 最后 : 计算self 的高度
//        if (i == _historyArr.count - 1) {
//            CGRect tempFrame = self.frame;
//            tempFrame.size.height = CGRectGetMaxY(_lastLabel.frame) + kTagVertical_margin;
//            self.frame = tempFrame;
//        }
//    }
//}


#pragma mark -- 文本属性设置
- (void)configLabel:(UILabel *)tag {
    
    tag.layer.borderWidth = 1.0;
    tag.layer.borderColor = [UIColor blackColor].CGColor;
    tag.layer.cornerRadius = 3;
    tag.layer.masksToBounds = YES;
    tag.userInteractionEnabled = YES;
   
    tag.textAlignment = NSTextAlignmentCenter;
    
    //给文本添加点击
    UITapGestureRecognizer *tapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchSubTagView:)];
    tapOne.numberOfTapsRequired = 1.0;
    [tag addGestureRecognizer:tapOne];
}


#pragma mark -- 点击文本
//-(void)touchSubTagView:(UITapGestureRecognizer*)tapOne
//{
//    UILabel *tag = (UILabel *)tapOne.view;
//    XJLog(@"==========%@",tag.text);
//
//    if (self.clickTagBlock) {
//        self.clickTagBlock(tag.text);
//    }
//}


- (void)clickLbGesgure:(UITapGestureRecognizer *)tap {
    UILabel *lb =    (UILabel *)tap.view;
    XJLog(@"%@ - %ld",lb.text ,lb.tag);
    
    XJCommunityModel *model = self.historyArr[lb.tag];
    if (self.clickTagBlock) {
        //用户id
        self.clickTagBlock(model);
    }
}


#pragma mark - 删除历史记录
- (void)deleteButtonAction {
    XJLog(@"删除历史记录");
    
    if (self.deleteHistoryBlock) {
        self.deleteHistoryBlock();
    }
}
@end
